package com.wteam.controller;


import cn.hutool.core.bean.BeanUtil;
import com.wteam.PoJo.DO.Address;
import com.wteam.PoJo.DTO.AddressDTO;
import com.wteam.PoJo.DTO.UpdateAddressDTO;
import com.wteam.PoJo.VO.AddressVO;
import com.wteam.PoJo.VO.signVO;
import com.wteam.Service.IAddressService;
import com.wteam.until.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址模块")
public class AddressController {
    @Autowired
    private IAddressService iAddressService;

    @ApiOperation(value = "添加收货地址")
    @PutMapping("/saveaddress")
    public Result SaveAddress(@Validated @RequestBody AddressDTO addressDTO) {
        return iAddressService.saveAddress(addressDTO);
    }

    @ApiOperation(value = "更改默认地址")
    @GetMapping("/update/{id}")
    public Result updateAddressdefault(@ApiParam("地址id") @PathVariable Long id) {
        return iAddressService.updateAddressdefault(id);
    }

    @ApiOperation(value = "根据地址id查地址信息")
    @GetMapping("/addressgetbyid/{id}")
    public Result<AddressVO> addressgetbyid(@ApiParam("地址id") @PathVariable Long id) {
        Address address = iAddressService.getById(id);
        AddressVO addressVO = BeanUtil.copyProperties(address, AddressVO.class);
        return Result.success(addressVO);
    }

    @ApiOperation(value = "查所有地址信息")
    @GetMapping("/alladdress")
    public Result<List<AddressVO>>alladdress() {
        return iAddressService.getalladdress();
    }

    @ApiOperation(value = "编辑地址信息")
    @PutMapping("/updateaddress")
    public Result updateaddress(@RequestBody @Validated UpdateAddressDTO addressDTO) {
        return iAddressService.updateaddress(addressDTO);
    }

    @ApiOperation(value = "删除地址信息")
    @DeleteMapping("/deleteaddress/{id}")
    public Result deleteaddress(@PathVariable @ApiParam("地址id") Long id) {
        return iAddressService.deletebyID(id);
    }

}
